package com.ciwong.epaper.modules.bookstore.adapter;

import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.clazz.bean.StudentBean;
import com.ciwong.epaper.modules.me.ui.GroupingStudentActivity;
import com.ciwong.epaper.widget.BaseBottomMenuDialog;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 学生分组适配器
 * @author: wyk
 * @time: 2021/5/24 17:26
 */
public class GroupingStuListAdapter extends BaseAdapter {

    //学生集合
    private List<StudentBean> studentBeanList;
    private GroupingStudentActivity mContext;
    private int classId;

    public GroupingStuListAdapter(GroupingStudentActivity mContext, List<StudentBean> studentBeanList, int classId) {
        this.mContext = mContext;
        this.studentBeanList = studentBeanList;
        this.classId = classId;
    }

    @Override
    public int getCount() {
        return null == studentBeanList ? 0 : studentBeanList.size();
    }

    @Override
    public Object getItem(int i) {
        return null == studentBeanList ? null : studentBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolderItem viewHolderItem;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_grouping_list_item, viewGroup, false);
            viewHolderItem = new ViewHolderItem();
            viewHolderItem.grouping_stu_name = view.findViewById(R.id.grouping_stu_name);
            viewHolderItem.stu_avatar = view.findViewById(R.id.stu_avatar);
            viewHolderItem.is_vip_img = view.findViewById(R.id.is_vip_img);
            viewHolderItem.stu_details = view.findViewById(R.id.stu_details);
            viewHolderItem.grouping_item_lay = view.findViewById(R.id.grouping_item_lay);
            view.setTag(viewHolderItem);
        } else {
            viewHolderItem = (ViewHolderItem) view.getTag();
        }
        viewHolderItem.grouping_stu_name.setText(studentBeanList.get(i).getUserName());
        if (studentBeanList.get(i).getIsVip() == 0) {
            viewHolderItem.is_vip_img.setVisibility(View.GONE);
        } else {
            viewHolderItem.is_vip_img.setVisibility(View.VISIBLE);
        }
        if (null != studentBeanList.get(i).getAvatar() && !"".equals(studentBeanList.get(i).getAvatar())) {
            Glide.with(mContext).load(studentBeanList.get(i).getAvatar()).into(viewHolderItem.stu_avatar);
        }
        viewHolderItem.stu_details.setText("账号:" + studentBeanList.get(i).getUserId() + "  " + studentBeanList.get(i).getIntegral() + "朵");
        viewHolderItem.grouping_item_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setBottomMenuDialog(i);
            }
        });
        return view;
    }

    private static class ViewHolderItem {
        private LinearLayout grouping_item_lay;
        private TextView grouping_stu_name;
        private ImageView stu_avatar;
        private ImageView is_vip_img;
        private TextView stu_details;

    }

    private BaseBottomMenuDialog bottomMenuDialog;

    private int childPosition;

    private List<String> menuOptionBeanList = new ArrayList<String>() {
        {
            add("复制账号");
            add("重置密码");
            add("清零花朵");
        }
    };

    public void setBottomMenuDialog(final int i) {
        this.childPosition = i;
        bottomMenuDialog = new BaseBottomMenuDialog(mContext);
        bottomMenuDialog.setTitle(studentBeanList.get(i).getUserName() + "(" + studentBeanList.get(i).getUserId() + ")");
        bottomMenuDialog.setItemList(menuOptionBeanList);
//        bottomMenuDialog.setOtherStyle(true);
        bottomMenuDialog.setOnItemClickListener(bgOnItemClickListener);
        bottomMenuDialog.show();
    }


    private AdapterView.OnItemClickListener bgOnItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            StudentBean mStudentBean = null;
            if (childPosition >= 0 && childPosition < studentBeanList.size()) {
                mStudentBean = studentBeanList.get(childPosition);
            }
            switch (position) {
                case 0:// 复制账号
                    copyUserName(mStudentBean);
                    bottomMenuDialog.dismiss();
                    break;
                case 1://重置密码
                    if (mStudentBean != null) {
                        mContext.setUserPassWord(classId, mStudentBean.getUserId(), mStudentBean.getUserName(), "Xx123456");
                    }
                    bottomMenuDialog.dismiss();
                    break;
                case 2://清零小红花
                    if (mStudentBean != null) {
                        mContext.showClearUserFlowerDialog(mStudentBean);
                    }
                    bottomMenuDialog.dismiss();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 复制学生名字
     *
     * @param mStudentBean
     */
    public void copyUserName(StudentBean mStudentBean) {
        if (mStudentBean != null) {
            ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(mStudentBean.getUserName() + " 账号:" + mStudentBean.getUserId());
            ToastUtil.INSTANCE.toastCenter(mContext, "复制成功 " + mStudentBean.getUserName() + " 账号:" + mStudentBean.getUserId());
        }
    }
}
